def patrol_task():
    """无参数巡逻任务，直接使用主程序中定义的全局变量：hwnd, left, top, window_width, window_height, model"""
    target_class_82 = 82  # 模块内固定参数
    confidence = 0.3  # 模块内固定置信度
    error_threshold = 5  # 模块内固定误差阈值
    dj_image_path = r'H:\zdh\yolo2\png\dj.png'  # dj图的PNG路径（需根据实际路径修改）

    while True:
        print("接到了巡逻任务")

        # 1. 巡逻核心动作（模块内固定参数）
        def action_C():
            movefb(hwnd, left, top, window_width, window_height, model,
                   要点击的目标类=10, offset_x=-50, offset_y=100,
                   置信度=confidence, click_type='left', error_threshold=error_threshold)
            movefb(hwnd, left, top, window_width, window_height, model,
                   要点击的目标类=10, offset_x=200, offset_y=-100,
                   置信度=confidence, click_type='left', error_threshold=error_threshold)

        run_action_until_image_disappears(
            hwnd=hwnd,
            model=model,
            image_path=r'H:\zdh\yolo2\png\jmgd.png',  # 模块内固定图像路径
            action=action_C,
            exit_action=lambda: True,
            confidence=confidence
        )

        # 2. 巡逻后的点击和快捷键操作（模块内固定类别5/6/10）
        def find_action_5():
            movefb(hwnd, left, top, window_width, window_height, model,
                   要点击的目标类=5, offset_x=-10, offset_y=-10,
                   置信度=confidence, click_type='left', error_threshold=error_threshold)

        find_and_perform_action(hwnd, model, 5, find_action_5)
        time.sleep(1)

        # ===================== 插入功能：循环执行 alt+e 直到 dj图出现（模板匹配） =====================
        max_retries = 30  # 最大重试次数（防止死循环）
        retry_interval = 0.5  # 每次重试间隔（秒），根据界面响应速度调整

        def check_dj_image_exists():
            """检测dj图是否存在（使用模板匹配）"""
            return run_action_until_image_disappears(
                hwnd=hwnd,
                model=model,  # 此处model可能不参与模板匹配，实际需调用找图模块的图片检测函数
                image_path=dj_image_path,
                action=lambda: None,  # 无动作，仅检测
                exit_action=lambda: True,
                confidence=confidence
            )  # 假设该函数返回True表示图片存在，False表示不存在（需根据模块实际逻辑调整）

        retry_count = 0
        while retry_count < max_retries:
            pyautogui.hotkey('alt', 'e')  # 执行快捷键
            time.sleep(retry_interval)    # 等待界面刷新

            # 检测dj图是否出现（假设找图模块返回True表示存在）
            if 检测框模块.image_exists(hwnd, dj_image_path, confidence=confidence):
                print("检测到 dj图，停止快捷键循环")
                break
            retry_count += 1
            print(f"未检测到 dj图，重试次数：{retry_count}/{max_retries}")

        if retry_count >= max_retries:
            print("超过最大重试次数，退出快捷键循环")
        # ===================== 插入功能结束 =====================

        movefb(hwnd, left, top, window_width, window_height, model,
               要点击的目标类=5, offset_x=-10, offset_y=-10,
               置信度=confidence, click_type='left', error_threshold=error_threshold)

        def accept_task_action():
            print("巡逻结束，接任务")
            movefb(hwnd, left, top, window_width, window_height, model,
                   要点击的目标类=10, offset_x=-10, offset_y=-30,
                   置信度=confidence, click_type='left', error_threshold=error_threshold)
            pyautogui.click()

        find_and_perform_action(hwnd, model, 6, accept_task_action)
        movefb(hwnd, left, top, window_width, window_height, model,
               要点击的目标类=6, offset_x=-10, offset_y=-10,
               置信度=confidence, click_type='left', error_threshold=error_threshold)

        # 3. 类别82检测（模块内固定检测逻辑）
        screenshot = 检测框模块.get_window_screenshot(hwnd)
        results = model(screenshot)
        has_class_82 = any(int(box.cls[0]) == target_class_82 for result in results for box in result.boxes)

        if has_class_82:
            print(f"检测到类别 {target_class_82}，重启巡逻")
            continue  # 重新进入循环
        else:
            print("未检测到类别82，退出巡逻模块")
            return  # 退出模块